const queryString = (name) => {
  let paramStr = location.search;
  paramStr = paramStr.substring(1);
  if (paramStr.length === 0) return null;

  paramStr = decodeURIComponent(paramStr);
  let params = paramStr.split('&');
  for (let i = 0; i < params.length; i++) {
    let parts = params[i].split('=');
    if (parts[0] === name) {
      return parts[1];
    }
  }
  return null;
}

/**
 * 获取url中的键值
 */
const queryData = (data, name) => {
  let paramStr = data;
  paramStr = paramStr.substring(paramStr.indexOf("?") + 1);
  if (paramStr.length === 0) return null;

  paramStr = decodeURIComponent(paramStr);

  let params = paramStr.split('&');
  for (let i = 0; i < params.length; i++) {
    let parts = params[i].split('=');
    if (parts[0] === name) {
      return parts[1];
    }
  }
  return null;
}

module.exports = {
  queryString: queryString,
  queryData: queryData
}